package com.zonski.jbots.engine;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: 4/06/2004
 * Time: 14:35:45
 * To change this template use Options | File Templates.
 */
public final class QuickVector
{
    private static final int DEFAULT_CAPACITY = 10;
    private static final int DEFAULT_GROW = 5;

    private Object[] objects;
    private int size;
    private int grow;

    public QuickVector()
    {
        this(DEFAULT_CAPACITY, DEFAULT_GROW);
    }

    public QuickVector(int capacity)
    {
        this(capacity, DEFAULT_GROW);
    }

    public QuickVector(int capacity, int grow)
    {
        this.objects = new Object[capacity];
        this.grow = grow;
    }

    public boolean contains(Object element)
    {
        for(int i=this.size; i>0; )
        {
            i--;
            if(element == this.objects[i])
            {
                return true;
            }
        }
        return false;
    }

    public void addElement(Object element)
    {
        ensureCapacity(this.size+1);
        this.objects[this.size] = element;
        this.size++;
    }

    public void setElementAt(Object element, int at)
    {
        this.objects[at] = element;
    }

    public void removeElementAt(int at)
    {
        int amt = this.size - at - 1;
        if(amt > 0)
        {
            System.arraycopy(this.objects, at+1, this.objects, at, amt);
            //System.arraycopy(this.work, 0, this.objects, at, amt);
        }else{
            this.objects[at] = null;
        }
        this.size--;
    }

    public void insertElementAt(Object element, int at)
    {
        this.ensureCapacity(this.size + 1);
        int amt = this.size - at;
        if(amt > 0)
        {
            System.arraycopy(this.objects, at, this.objects, at+1, amt);
        }
        this.objects[at] = element;
        this.size++;
    }

    public void removeElement(Object element)
    {
        for(int i=this.size; i>0; )
        {
            i--;
            if(this.objects[i] == element)
            {
                removeElementAt(i);
                //break;
            }
        }
    }

    public void removeAllElements()
    {
        // we should probably free up the objects in the vector
        for(int i=this.size; i>0; )
        {
            i--;
            this.objects[i] = null;
        }
        this.size = 0;
    }

    public Object elementAt(int at)
    {
        return this.objects[at];
    }

    public int size()
    {
        return this.size;
    }

    public void ensureCapacity(int capacity)
    {
        if(this.objects.length < capacity)
        {
            int newSize = this.objects.length + this.grow;
            Object[] work = new Object[newSize];
            System.arraycopy(this.objects, 0, work, 0, this.size);
            this.objects = work;
            //this.work = new Object[newSize];
        }
    }
}
